package xyz.scootaloo.oj.iq

import org.junit.Test

/**
 * @author flutterdash@qq.com
 * @since 2021/4/17 22:15
 */
class M1604 {

    fun tictactoe(board: Array<String>): String {
        val size = board.size
        val res = IntArray(8)
        var space = false

        for (idx in board.indices) {
            val line = board[idx]
            for (c in line) {
                when (c) {
                    'X' -> res[0]++
                    'O' -> res[1]++
                    ' ' -> space = true
                }
            }

            if (res[0] == size) return "X"
            if (res[1] == size) return "O"

            if (line[idx] == 'X')
                res[2]++
            else if (line[idx] == 'O')
                res[3]++

            if (line[size - 1 - idx] == 'X')
                res[4]++
            else if (line[size - 1 - idx] == 'O')
                res[5]++

            reset(res)

            for (column in board.indices) {
                when (board[column][idx]) {
                    'X' -> res[6]++
                    'O' -> res[7]++
                    ' ' -> space = true
                }
            }

            if (res[6] == size)
                return "X"
            if (res[7] == size)
                return "O"

            reset(res)
        }

        if (res[2] == size || res[4] == size)
            return "X"
        if (res[3] == size || res[5] == size)
            return "O"
        return if (space)
            "Pending"
        else
            "Draw"
    }

    private fun reset(res: IntArray) {
        res[0] = 0
        res[1] = 0
        res[6] = 0
        res[7] = 0
    }

    @Test
    fun test0() {
        val board = arrayOf(
            "OX ",
            "OX ",
            "O  "
        )
        println(tictactoe(board))
    }

    @Test
    fun test1() {
        val board = arrayOf(
            "                             ",
            "                     O       ",
            "         X           X       ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                      O      ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "                             ",
            "      X                      ",
            "                             ",
            "                             ",
            "      O                      ",
            "                             ",
            "                             "
        )
        println(tictactoe(board))
    }

}
